#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

const int N = 1010;
int n, m;
int p[N];

struct edge
{
	int a, b, w;

	bool operator<(const edge& W)
	{
		return w < W.w;
	}
}edges[N];

int find(int x)
{
	if (x != p[x]) p[x] = find(p[x]);
	return p[x];
}

int main()
{
	scanf("%d%d", &n, &m);
	int tt = 0;
	for (int i = 1; i <= m; ++i)
		for (int j = 1; j <= m; ++j)
		{
			cin >> edges[++tt].w;
			edges[tt].a = i, edges[tt].b = j;
			if (edges[tt].w == 0) edges[tt].w = n;
		}
	for (int i = 1; i <= i; ++i) p[i] = i;
	sort(edges + 1, edges + 1 + tt);

	int res = n;
	for (int i = 1; i <= tt; ++i)
	{
		int a = edges[i].a, b = edges[i].b, w = edges[i].w;

		a = find(a), b = find(b);
		if (a != b)
		{
			p[a] = b;
			res += w;
		}
	}
	cout << res;
	return 0;
}